<?php

namespace common\models;

use common\consts\Consts;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Task;

/**
 * TaskSearch represents the model behind the search form about `common\models\Task`.
 */
class TaskSearch extends Task
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'priority', 'is_synchronization', 'status', 'suspend_times', 'retry_times', 'total_number', 'finished_number'], 'integer'],
            [['type', 'key', 'from_system', 'request_data', 'response_data', 'memo', 'next_run_date', 'started_at', 'stopped_at', 'finish_rate', 'created_by', 'created_at', 'updated_by', 'updated_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Task::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => Consts::DEFAULT_PAGE_SIZE,
            ],
            'sort'=> ['defaultOrder' => ['updated_at'=>SORT_DESC]],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'priority' => $this->priority,
            'is_synchronization' => $this->is_synchronization,
            'status' => $this->status,
            'next_run_date' => $this->next_run_date,
            'suspend_times' => $this->suspend_times,
            'retry_times' => $this->retry_times,
            'started_at' => $this->started_at,
            'stopped_at' => $this->stopped_at,
            'total_number' => $this->total_number,
            'finished_number' => $this->finished_number,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'type', $this->type])
            ->andFilterWhere(['like', 'key', $this->key])
            ->andFilterWhere(['like', 'from_system', $this->from_system])
            ->andFilterWhere(['like', 'request_data', $this->request_data])
            ->andFilterWhere(['like', 'response_data', $this->response_data])
            ->andFilterWhere(['like', 'memo', $this->memo])
            ->andFilterWhere(['like', 'finish_rate', $this->finish_rate])
            ->andFilterWhere(['like', 'created_by', $this->created_by])
            ->andFilterWhere(['like', 'updated_by', $this->updated_by]);

        return $dataProvider;
    }
}
